﻿using UnityEngine;
using System.Collections;

public class SetupMenuLogic : SQMenuBase
{
    public UISlider musicSlide;
    public UISlider soundSlide;
    public GameObject _setMainMenu;
    public GameObject[] btnSwitch;
    AudioSource mainMenuMusic;
    public override void onStart()
    {
        base.onStart();
        SparkExtensions.SetChildText(_setMainMenu, "BasicSetup/txt_PlayerName", GlobalDataManager.instance.playerData.userName);
        SparkExtensions.SetChildText(_setMainMenu, "About/txt_userName", GlobalDataManager.instance.playerData.userName+"先生");
        mainMenuMusic = SQManager.Instance.GetComponent<AudioSource>();
        musicSlide.value = mainMenuMusic.volume;
        soundSlide.value = SQManager.Instance.soundManager.initSound;
        NotificationCenter.DefaultCenter().AddObserver(this, "a");
        foreach (GameObject btn in btnSwitch) 
        {
            UIEventListener.Get(btn).onClick = setSwitch;
        }
    }
    void a()
    {
        Debug.Log("" + 1);
    }
    public override void OnClick(GameObject btn)
    {
        switch (btn.name)
        {
            case "btnSetupClose":
                btn.gameObject.GetComponent<UIPlaySound>().Play();
                UIManager.Instance.unpopMenu();
                break;
            default:
                break;
        }
    }
    public void musicVolumeControl()
    {
       mainMenuMusic.volume = musicSlide.value;

    }
    public void soundVolumeControl()
    {
        SQManager.Instance.soundManager.initSound = soundSlide.value;
        UIPlaySound[] sounds = FindObjectsOfType<UIPlaySound>();
        foreach(UIPlaySound sound in sounds){
            sound.volume = soundSlide.value;
        }
    }
    /// <summary>
    /// 设置震动，牌局聊天，牌局动画的开关
    /// </summary>
    /// <param name="btn"></param>
    public void setSwitch(GameObject btn) 
    {
        switch (btn.name)
        {
            case "btnShakeInduce":
                btnSwitch[0].SetActive(false);
                btnSwitch[1].SetActive(true);
                break;
            case "btnShakeFork":
                btnSwitch[0].SetActive(true);
                btnSwitch[1].SetActive(false);
                break;
            case "btnChatInduce":
                btnSwitch[2].SetActive(false);
                btnSwitch[3].SetActive(true);
                break;
            case "btnChatFork":
                btnSwitch[2].SetActive(true);
                btnSwitch[3].SetActive(false);
                break;
            case "btnCardAniInduce":
                btnSwitch[4].SetActive(false);
                btnSwitch[5].SetActive(true);
                break;
            case "btnCardAniFork":
                btnSwitch[4].SetActive(true);
                btnSwitch[5].SetActive(false);
                break;
            case "btnBatterySaveInduce":
                setPowerSwitch(false);
                musicSlide.gameObject.GetComponent<UISlider>().value = 0;
                soundSlide.gameObject.GetComponent<UISlider>().value = 0;
                break;
            case "btnBatterySaveFork":
                setPowerSwitch(true);
                musicSlide.gameObject.GetComponent<UISlider>().value = SQManager.Instance.soundManager.initSound;
                soundSlide.gameObject.GetComponent<UISlider>().value = SQManager.Instance.soundManager.initSound;
                break;
        }
    }
    /// <summary>
    /// 设置省电模式的开关
    /// </summary>
    /// <param name="current"></param>
    public void setPowerSwitch(bool current) 
    {
        for (int i = 0; i < btnSwitch.Length; i++)
        {
            if (i % 2 == 0)
            {
                btnSwitch[i].SetActive(current);
            }
            else
            {
                btnSwitch[i].SetActive(!current);
            }
        }
    }
    // Press button to vibrate
    //触发振动
    public void onShake()
    {
            //iPhoneUtils.Vibrate();
            Handheld.Vibrate();
    }
 


}
